package com.kvest.fragmentsfinaltest.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TimeUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.extras.SoundPullEventListener;
import com.kvest.fragmentsfinaltest.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 24.03.13
 * Time: 17:12
 * To change this template use File | Settings | File Templates.
 */
public class PullToRefreshListFragment extends Fragment implements OnRefreshListener<ListView> {
    private ArrayAdapter<String> adapter;
    private PullToRefreshListView pullToRefreshListView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.pull_to_refresh_list_fragment_layout, null);
        pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.list_ptr);
        registerListeners(pullToRefreshListView);

        //create and set adapter
        adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1,
                                           new ArrayList<String>());
        ListView list = pullToRefreshListView.getRefreshableView();
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String str = "selected: " + adapter.getItem(i- 1);
                Toast.makeText(getActivity().getApplicationContext(), str, Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

    private void registerListeners(PullToRefreshListView pullToRefreshListView) {
        pullToRefreshListView.setOnRefreshListener(this);

        pullToRefreshListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {

            @Override
            public void onLastItemVisible() {
                Toast.makeText(getActivity().getApplicationContext(), "End of List!", Toast.LENGTH_SHORT).show();
            }
        });

        /**
         * Add Sound Event Listener
         */
        SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>(getActivity());
        soundListener.addSoundEvent(PullToRefreshBase.State.PULL_TO_REFRESH, R.raw.pull_event);
        soundListener.addSoundEvent(PullToRefreshBase.State.RESET, R.raw.reset_sound);
        soundListener.addSoundEvent(PullToRefreshBase.State.REFRESHING, R.raw.refreshing_sound);
       // pullToRefreshListView.setOnPullEventListener(soundListener);
    }

    @Override
    public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        new Loader().execute();
    }

    private class Loader extends AsyncTask<Void, Void, String> {
        @Override
        protected String doInBackground(Void... params) {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException interruptedException) {};

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
            return sdf.format(new Date());
        }

        @Override
        protected void onPostExecute(String result) {
            adapter.add(result);
            adapter.notifyDataSetChanged();

            pullToRefreshListView.onRefreshComplete();
        }
    }
}
